var bdc = {};
bdc.recordNum = 0;
bdc.signup = false;
bdc.command = {
	openPasswordModal:function(){		
		$('#passwordModal').modal('show');
		$("#password-editinput").val("");
		$("#new-password-editinput").val("");
		$("#new-password2-editinput").val("");
		$('#passwordModal').on('shown', function() {
			$("#password-editinput").focus();
		})
	},
	changepsHandler:function(url){
		var _this = this;
		$("#changeps").click(function(){

			_this.openPasswordModal()

		})
		$("#passwordsave").click(function(){

			var ps = $("#password-editinput"),
				newps = $("#new-password-editinput"),
				newps2 = $("#new-password2-editinput");


			if(ps.val()==""){

				ps.after("<span>请输入密码。</span>")
				ps.focus()

			}else if(newps.val()==""){
				newps.after("<span>请输入新密码。</span>")
				newps.focus()
			}else if(newps2.val()==""){
				newps2.after("<span>请输入确定密码。</span>")
				newps2.focus()
			}else if(newps.val()!=newps2.val()){
				newps2.after("<span>两次输入密码不一致，请重新输入。</span>")
				newps2.focus()
			}else{
				$.ajax({
				type : "POST",
				url : url,
				data : {
				password : ps.val()

			},
			success : function(data) {
				if(data == "true") {

					$('#passwordModal').modal('hide');
				}else{
					alert("修改密码出错，请重试。")
				}

			}
		})

			}

			


		})

	}
}
bdc.login = {

	handler : function(url) {
		var _this = this;
		$(".placeholder").click(function() {
			$(this).prev().focus()
			$(this).animate({
				opacity : 0.2
			});
		})
		$("#loginform").submit(function(){
			
			return _this.loginCheck()
			
		})
		
		$("#sign-submit").click(function(){
			
			_this.signupSubmit(url)
			
		})
		$(".text-input").blur(function() {

			if($(this).val() == "") {

				$(this).next().animate({
					opacity : 1
				});

			} else {

				$(this).next().hide()

			}

		}).keydown(function() {

			$(this).next().hide()

		})
		_this.signupCheck(url)
	},
	checkEmail : function(el) {

		var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z-]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]|net|NET|asia|ASIA|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT|cn|CN|cc|CC)$"
		var re = new RegExp(regu);
		if(el.search(re) == -1) {
			return true;
			//非法
		}
			return false;
		//正确
	},
	loginCheck:function(){
		
		var $loginmail = $("#login-mail"),
			$loginpassword = $("#login-password"),
			_this = this;
			if(_this.checkEmail($loginmail.val())){
				_this.showError("Email格式不对，请重新填写。");
				$loginmail.focus();
				return false;
			}else if($loginpassword.val()==""){
				_this.showError("请输入密码。");
				$loginpassword.focus();
				return false;				
			}else{
				return true;
				//$("#loginform").submit();
				
			}
				
	},
	forgetCheck:function(){

		

	},
	forgetHandler:function(){
			var $mail = $("#mail"),
				_this =this;

			$("#forgetform").submit(function(){	
			if(_this.checkEmail($mail.val())){

				$("#alerterror").remove()
				var errText = '<div  class="alert alert-error alerterror" style="top:70px;"><a class="close" data-dismiss="alert" href="#">×</a><h4 class="alert-heading">邮件格式不正确，请重新输入。</h4></div>'
				$(".alert-main").before(errText);
				$mail.focus();
				return false;

			}
			else{

				return true				
			}

			})


	},

	signupCheck:function(url){
		var $mail = $("#signup-mail"),
			$password = $("#signup-ps"),
			$password2 = $("#signup-ps2"),
			_this = this;
			/*$mail.blur(function(){
				if(_this.checkEmail($(this).val())){
				_this.showError("注册电子邮件格式不对，请重新填写。");
				bdc.signup = false;
				}else{
				$.get(url,{mail:$mail.val()}, function(data){
  						if(data=="false"){
  							_this.showError("电子邮件地址已经被注册，请重新填写。");
  							bdc.signup = false;
  						}else{
  							bdc.signup = true;  							
  						}
						});				
				}	
				
			})	*/	
	},
	signupSubmit:function(url){
		var $mail = $("#signup-mail"),
			$password = $("#signup-ps"),
			$password2 = $("#signup-ps2"),
			_this  =this;
			if($mail.val()==""){
				_this.showError("请输入电子邮件地址。");
				$mail.focus();
				bdc.signup = false;
				return false;
				
			}else{
				if(_this.checkEmail($mail.val())){
				_this.showError("注册电子邮件格式不对，请重新填写。");
				$mail.focus();
				bdc.signup = false;
				return false;
				}else{
				$.get(url,{mail:$mail.val()}, function(data){
  						if(data=="false"){
  							_this.showError("电子邮件地址已经被注册，请重新填写。");
  							bdc.signup = false;
  						}else{
  							bdc.signup = true;  
  							if($password.val()==""){
				_this.showError("请输入密码。");
				$password.focus();
				bdc.signup = false;
				return false;
			}else if($password2.val()==""){
				_this.showError("请输入确认密码。");
				$password2.focus();
				bdc.signup = false;
				return false;
			}else if($password.val() != $password2.val()){		
				_this.showError("两次输入密码不一致，请重新输入");
				bdc.signup = false;
				return false;
			}else{
				bdc.signup = true;				
			}

				$("#signform").submit()							
  						}
						});				
				}
				
			}
			
			
			

		
	},
	showError:function(msg){
		$("#loginerror").remove()
		var errText = '<div  class="alert alert-error loginerror"><a class="close" data-dismiss="alert" href="#">×</a><h4 class="alert-heading">' + msg + '</h4></div>'
		$(".login-form").before(errText)
	}
	
}
bdc.record = {

	tagSelect : function() {

		var $tagItems = $(".tagMenu");
		$tagInput = $("#tag-input");

		$tagItems.live("click", function() {

			var _text = $(this).text()
			$tagInput.val(_text);
			$tagInput.next().hide()

		})
	},
	focusHandler : function() {
		$(".placeholder").click(function() {
			$(this).prev().focus()
			$(this).animate({
				opacity : 0.2
			});
		})
		$("textarea,input").blur(function() {
			if($(this).val() == "") {

				$(this).next().show().animate({
					opacity : 1
				});

			} else {

				$(this).next().hide()

			}

		}).keydown(function() {

			$(this).next().hide()

		})
	},
	save : function(url) {
		var _this = this, $memory = $("textarea[name='memory']"), $description = $("textarea[name='description']");

		$("#save-btn").click(function() {
				var $this = $(this);
			if($this.hasClass("Bootstrap")){

					return false;

			}
			if($memory.val() == "") {

				_this.showError("请输入需要记忆的内容!");
				window.setTimeout(function() {
					$memory.focus()
				}, 300)
				return false;

			}
			$(".alert").alert('close')
			$this.addClass("disabled")
			$.ajax({
				type : "POST",
				url : url,
				dataType : "json",
				data : {
					tag : $("#tag-input").val(),
					memory : $memory.val(),
					description : $description.val()
				},
				success : function(data) {
					if(data.state == "true") {

						$description.val("").next().show().animate({
							opacity : 1
						});
						$memory.val("").focus();
						_this.showBadge();

						_this.showSuccess("保存成功，您可以继续添加记忆卡片。")
						_this.addTagSelect(data.tag)
					}
					$this.removeClass("disabled")
				},
				error:function(){
					_this.showError("保存失败，请重试。")
					$this.removeClass("disabled")	
				}
			})

		})
	},
	addTagSelect : function(tags) {
		var select = '<div class="btn-group" style="margin-bottom:-7px;"><a class="btn dropdown-toggle btn-inverse" data-toggle="dropdown" id="tag-select" href="#">选择分类<span class="caret"></span></a><ul class="dropdown-menu" id="tag-menu"></ul></div></div>', menu = "";

		$.each(tags, function(i, n) {
			menu += '<li><a href="#" class="tagMenu">' + n + '</a></li>';

		})
		$("#tag-select").parent().remove()
		$("#tag-input").next().after(select);
		$("#tag-menu").html(menu)
	},
	showError : function(text) {

		var errText = '<div  class="alert alert-error" style="position: absolute;width: 751px;"><a class="close" data-dismiss="alert" href="#">×</a><h4 class="alert-heading">' + text + '</h4></div>'

		$(".front-container").prepend(errText)

	},
	showSuccess : function(text) {
		var successText = '<div  class="alert alert-success" style="position: absolute;width: 751px;"><a class="close" data-dismiss="alert" href="#">×</a>' + text + '</div>'
		$(".front-container").prepend(successText);
		window.setTimeout(function() {
			//$(".alert").alert('close')
		}, 2000)
	},
	showBadge : function() {
		$(".badge").remove()
		var Str = '<span class="badge badge-warning" style="display: block; position: absolute; top:15px;left:250px;z-index:100">+' + (bdc.recordNum + 1) + '</span>';
		$(".nav").before(Str);
		$(".badge").effect("bounce", {}, 500);
		bdc.recordNum++;

	}
}

bdc.lib = {

	delRecoder : function(id, url) {

		$.ajax({
			type : "POST",
			url : url,
			data : {
				recordId : id
			},
			success : function(data) {

				if(data == "true") {

					$("#recoder" + id).effect("fade");

				}

			}
		})

	},
	delRecoderHandler : function(url) {
		var _this = this;
		$(".library-list-item").find(".close").click(function() {

			_this.delRecoder($(this).attr("recordId"), url)

		})
	},
	editRecoderHandler : function() {

		//onclick="$('#myModal').modal('show')"
		var _this = this, $items = $(".library-list-item"), $items_title = $items.find("h4").find("a");

		$items_title.click(function() {
			var _id = $(this).attr("recordId"), $recoder = $("#recoder" + _id), _memory = $recoder.find("h4").text(), _description = $recoder.find("p").text(), _tag = $(this).attr("tag");
			//alert(_this._getAllTag())
			//$('#myModal').modal('show')
			_this._recoderModalShow(_tag, _this._getAllTag(), _memory, _description, _id)

		})
		$items.hover(function() {
			$(this).addClass("library-list-item_hover")
		}, function() {
			$(this).removeClass("library-list-item_hover")
		})
		$("#tag-menu").find("a").live("click",function(){

			$("#tag-input").val($(this).html())

		})
	},
	_recoderModalShow : function(tag, alltag, memory, description, recoderid) {
		$("#recoder-id").val(recoderid);
		$("#description-input").val(description);
		$("#tag-input").val(tag);
		$('#recoderModal').modal('show');
		$("#memory-input").val(memory);
		$("#tag-menu").html("")
		$.each(alltag, function(i, n) {

			$("#tag-menu").append('<li><a href="#">' + n + '</a></li>')

		})
	},
	_getAllTag : function() {

		var $allTag = $("#allTag").find("a"), tagArray = [];

		$allTag.each(function() {
			if($(this).text() !== "未分类") {
				tagArray.push($(this).text())
			}

		})
		return tagArray;

	},
	updateRecoder : function(url) {
		$("#updaterecoderbtn").click(function() {
			var _recoderId = $("#recoder-id").val(), 
			_memory = $("#memory-input").val(),
			_description = $("#description-input").val(),
			 _tag = $("#tag-input").val(), 
			 $recoder = $("#recoder" + _recoderId), 
			 $memory = $recoder.find("h4").find("a"), 
			 $description = $recoder.find("p"), 
			 $tag = $recoder.find(".label");

			$.ajax({
				type : "POST",
				url : url,
				dataType : "json",
				data : {
					recordId : _recoderId,
					memory : _memory,
					description : _description,
					tag : _tag

				},
				success : function(data) {
					if(data.state == "true") {
						if($recoder.find(".label").length==0){


							if($memory.attr("tag")!=_tag){

									$("#recoder"+_recoderId).remove()

							}

						}else{

							$memory.attr("tag",_tag)	
						}
						$memory.text(_memory);
						
						$description.text(_description);
						$tag.text(_tag);
						$('#recoderModal').modal('hide')
					}

				}
			})

		})
	}
}
bdc.memory = {

	btnHandler : function() {

		var $forget = $("#forget"), $yes = $("#ok"), $no = $("#no"), $memory = $("#memory");
		$description = $("#description");
		$no.hide();
		$description.hide()
		$forget.click(function() {
			$memory.addClass("review-console-alert", 1000);
			$description.slideDown("500");
			$yes.hide();
			$(this).hide();
			$no.show();
		})
	}
}
bdc.lib.tag = {

	addTag : function(url) {
		var _name = $("#tag-editinput").val()
		$.ajax({
			type : "POST",
			url : url,
			data : {
				tagName : _name

			},
			success : function(data) {
				if(data == "true") {

					document.location.reload()
				}

			}
		})

	},
	editTag : function(url) {
		var _id = $("#tag-id").val(), _name = $("#tag-editinput").val(), _oldname = $("#tagHeader").find("span").html(), _oldArr = ["未分类"];

		$.each(bdc.lib._getAllTag(), function(i, n) {

			if(n !== _oldname) {

				_oldArr.push(n);

			}

		})
		for(var i = 0; i < _oldArr.length; i++) {

			if(_oldArr[i] == _name) {

				alert("分类名称重复，请重新输入。");
				_name.focus()
				return false;

			}

		}
		$.ajax({
			type : "POST",
			url : url,
			data : {
				tagId : _id,
				tagName : _name

			},
			success : function(data) {

				if(data == "true") {

					$(".library-container").find("li.active").find("a").html("" + _name + "<i class='icon-chevron-right pull-right'></i>")
					$("#tagHeader").find("span").html(_name);
					$('#tagModal').modal('hide');
				}

			}
		})

	},
	delTag : function(url) {
		var _id = $("#tag-id").val();
		$.ajax({
			type : "POST",
			url : url,
			data : {
				tagId : _id

			},
			success : function(data) {

				if(data == "true") {

					document.location.reload()

				}

			}
		})

	},
	openTagModal : function(type, id, name) {
		if(type == "add") {
			$("#tagModal").find("h3").html("添加分类")
			$("#delTag").hide();
			$("#addtagbtn").show();
			$("#updatetagbtn").hide()
		} else {
			$("#tagModal").find("h3").html("修改分类")
			$("#delTag").show();
			$("#tag-id").val(id);
			$("#tag-editinput").val(name);
			$("#addtagbtn").hide();
			$("#updatetagbtn").show()
		}
		$('#tagModal').modal('show');
		$('#tagModal').on('shown', function() {
			$("#tag-editinput").focus();
		})
	},
	handler : function(update, del, add) {
		var _this = this;
		$("#tagEditBtn").click(function() {

			_this.openTagModal("edit", $(this).attr("tagId"), $("#tagHeader").find("span").html())

		})
		$("#updatetagbtn").click(function() {

			_this.editTag(update)

		})
		$("#delTag").click(function() {

			_this.delTag(del)

		})
		$("#addTag").click(function() {

			_this.openTagModal("add")

		})
		$("#addtagbtn").click(function() {

			_this.addTag(add)

		})
	}
}